package com.example.springbootdemo.controller;

import com.alibaba.fastjson.JSON;
import com.example.springbootdemo.model.Result;
import com.example.springbootdemo.model.UserModel;
import com.example.springbootdemo.util.ResultSetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@CrossOrigin
@Slf4j
public class FilterController {

    @Autowired
    DataSource dataSource;
    Statement statement;



    @PostConstruct
    public void initDatasource() {
        log.info("初始化数据库连接");
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            statement = connection.createStatement();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }


    @RequestMapping("/getList")
    public Result getList() throws IOException {
        log.info("getList start");
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            String sql = "select * from ft_result1 limit 10";
            ResultSet resultSet = statement.executeQuery(sql);
            Map<String, Object> rowMap;
            while ((rowMap = ResultSetUtil.getNextRow(resultSet)) != null) {
                list.add(rowMap);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return Result.success(list);
    }

    @RequestMapping("/getListPageSize")
    public Result getListPageSize(@RequestBody Map<String, Object> inputMap) throws IOException, SQLException {
        log.info("getListPageSize start");
        int size = inputMap.get("pageSize") == null ? 0 : Integer.parseInt(String.valueOf(inputMap.get("pageSize"))); // 显示条数
        int page = inputMap.get("currentPage") == null ? 0 : (Integer.parseInt(String.valueOf(inputMap.get("currentPage"))) - 1) * size; // 当前页
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> outpuMap = new HashMap<>();
        int count = 0;
        String countSql = "select count(1) as count from ft_result1";
        ResultSet resultSet1 = statement.executeQuery(countSql);
        if (resultSet1.next()) {
            count = resultSet1.getInt("count");
        }
        try {
            String sql = "select * from ft_result1 limit " + page + "," + size;
            ResultSet resultSet = statement.executeQuery(sql);
            Map<String, Object> rowMap;
            while ((rowMap = ResultSetUtil.getNextRow(resultSet)) != null) {
                list.add(rowMap);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        outpuMap.put("data", list);
        outpuMap.put("count", count);
        return Result.success(outpuMap);
    }


    @PostMapping("/test2")
    public Result test2(@RequestBody UserModel userModel, @RequestHeader HttpHeaders httpHeaders) {
        List<String> list1 = httpHeaders.get("Authorization");
        UserModel userModel1 = JSON.parseObject(JSON.toJSONString(userModel), UserModel.class);
        System.out.println(userModel1);
        return Result.success("success");
    }


}
